package com.xiao.pool;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * @author Xiao
 * @version 1.0
 * @description 抽象数据源
 * @date 2022/1/2
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public abstract class MyAbstractDataSource implements MyDataSourceInterface{

    private String username;
    private String password;
    private String driver;
    private String url;
    //最大空闲连接数
    private final int poolMaxIdleSize = 10;
    //最大激活连接数
    private final int poolMaxActiveSize = 10;
    //获取连接最长等待时间
    private final long poolTimeToWait = 30000L;

    @Override
    public Connection getConnection() throws SQLException {
        return this.getConnection(username, password);
    }

    @Override
    public Connection getConnection(String username, String password) throws SQLException {
        return doGetConnection(username, password);
    }

    /**
     * 获取数据库连接对象
     * @param username
     * @param password
     * @throws SQLException
     * @return
     */
    private Connection doGetConnection(String username, String password) throws SQLException {
        return DriverManager.getConnection(url, username, password);
    }
}
